/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import dao.DaoUsuario;
import entidades.Usuario;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import util.Global;
import util.Upload;
import util.Util;

/**
 *
 * @author a968340
 */
@WebServlet(name = "ServletEdUsuario", urlPatterns = {"/ServletEdUsuario"})
public class ServletEdUsuario extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        Upload upload = new Upload(getServletContext().getRealPath(Global.usuarioData_path));
        List list = upload.processRequest(request);
        Map<String, String> map = upload.getFormValues(list);
        String login = map.get("login");
        String nome = map.get("nome");
        String senha1 = map.get("senha1");
        String senha2 = map.get("senha2");
        String imagem = map.get("imagem");
        String dtNasc = map.get("dtNasc");


        if (Util.valida(Util.CAMPO_TEXTO, nome) && Util.valida(Util.CAMPO_TEXTO, login) && Util.valida(Util.CAMPO_DATA, dtNasc)
                && Util.valida(Util.CAMPO_TEXTO, senha1) && Util.valida(Util.CAMPO_TEXTO, senha2)) {
            if (senha1.equals(senha2)) {
                Date nascimento = null;
                try {
                    nascimento = Util.validaData(dtNasc);
                } catch (ParseException ex) {
                    Logger.getLogger(ServletEdUsuario.class.getName()).log(Level.SEVERE, null, ex);
                }
                Usuario usuario = (Usuario) session.getAttribute("usuario");
                usuario.setLogin(login);
                usuario.setNome(nome);
                usuario.setSenha(senha1);
                usuario.setImagem(imagem);
                usuario.setDtNasc(nascimento);
                new DaoUsuario().update(usuario);

                RequestDispatcher rd = request.getRequestDispatcher("perfil.jsp");
                rd.forward(request, response);
            } else {
                request.setAttribute("erro", "As senhas não podem ser diferentes");
                RequestDispatcher rd = request.getRequestDispatcher("edPerfil.jsp");
                rd.forward(request, response);
            }
        } else {
            request.setAttribute("erro", "Nenhum dos campos pode ser vazio");
            RequestDispatcher rd = request.getRequestDispatcher("edPerfil.jsp");
            rd.forward(request, response);
        }
    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
